﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace ArivisTests.Models
{
    public class ArivisTestsDBContext : DbContext
    {
        public ArivisTestsDBContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<TestPlan> TestPlans { get; set; }

        public DbSet<TestScenario> TestScenarios { get; set; }

        public DbSet<TestCase> TestCases { get; set; }

        public DbSet<TestRun> TestRuns { get; set; }

        public DbSet<UserProfile> UserProfiles { get; set; }

        public DbSet<TestResult> TestResults { get; set; }

        public IQueryable<UserProfile> ActiveTester
        {
            get
            {
                string[] tester = System.Web.Security.Roles.GetUsersInRole(KnownRoles.Tester);
                return UserProfiles.Where(user => tester.Contains(user.UserName));
            }
        }

        public IQueryable<TestScenario> TestScenariosWithOwners
        {
            get
            {
                return this.TestScenarios.Include(scenario => scenario.Owner);
            }
        }

        public IQueryable<TestRun> TestRunsWithDetails
        {
            get
            {
                IQueryable<TestRun> result = this.TestRuns;
                result = result.Include(run => run.Tests.Select(test => test.Result));
                result = result.Include(run => run.Tests.Select(test => test.Scenario));
                result = result.Include(run => run.Tests.Select(test => test.User));
                return result;
            }
        }

        public IQueryable<TestRun> TestRunsWithUsers
        {
            get
            {
                IQueryable<TestRun> result = this.TestRuns;
                result = result.Include(run => run.Tests.Select(test => test.User));
                return result;
            }
        }
    }
}